#作业三：回合制对战游戏，创建角色：名字，姓别，血量，装备，战斗
#创建房间：角色可加入房间，房间内的人可以互打，房间上限2人。
#初始化：装备-防弹衣，血量默认100。正常被攻击-10，有防弹衣-5
###创建异常机制，用来判断用户创建角色时是否符合设定(装备为True，False，名字为6-10位，性别为男或女。血量不需要输入，系统默认是100)
class InputException(Exception):
    def __str__(self):
        return "用户创建不合规范"
####创建用户加入房间异常，用户加入用户2创建的房间，如果用户2没有创建房间，则给与提醒。若用户1已经创建房间，则不允许加入房间。
class JoinException(Exception):
    def __str__(self):
        return "加入房间不存在或玩家已经创建房间"
class Player(object):
    count=0   #统计总玩家个数
    #初始化
    def __init__(self,name,sex,equ):   #传递姓名，性别，是否有装备
        try:
            if 6<=len(name)<=10 and sex in ["男","女"] and equ in  [0,1]:
                self.name = name
                self.sex = sex
                self.blood = 100
                self.equ = equ
                Player.count+=1
            else:
                raise InputException
        except InputException as e:
            print("自定义异常",e)

    #输出玩家的信息
    def _info(self):
        print(f"玩家{Player.count}的姓名为:{self.name},性别{self.sex},血量{self.blood},装备情况{self.equ}")

    #创建房间,输入房间名
    def create_room(self,room_name):
        self.room_player=[self]
        self.room_name=room_name
        print(f"玩家{self.name}创建房间成功！")

    #加入房间,指定加入哪个用户创建的房间
    def join_room(self,player):
        try:
            if  hasattr(self,"room_player")==True or hasattr(player,"room_player")==False:
                raise JoinException
            else:
                room_len = len(player.room_player)
                if room_len==1:
                    player.room_player.append(self)
                    print(f"玩家{self.name}和玩家{player.name}配对成功,房间{player.room_name}组队成功，可以开始battle")
                else:
                    print("该房间已满，不允许加入！")
        except JoinException as e:
            print(f"玩家{self.name}加入房间异常",e)

    # 打斗
    def battle(self, player):
        def ha(play):
            if play.equ == 1:
                hart = 5
            else:
                hart = 10
            play.blood -= hart
            #print(f"{play.blood}，{play.name}")
        while self.blood!=0 and player.blood!=0:   #回合制战斗
            ha(player)   #先是玩家一打玩家二
            ha(self)     #玩家二打玩家一
        if self.blood==0:
            print(f"玩家{self.name}被玩家{player.name}KO")
        else:
            print(f"玩家{player.name}被玩家{self.name}KO")
player4=Player("zhangli","女","gsr")
player1=Player("huangxinyi","女",1)
#player1._info()
player2=Player("zhaiwenxiu","男",0)
#player2._info()
player1.create_room("hxy_zwx")
player2.join_room(player1)
player2.battle(player1)

##验证房间异常机制
player3=Player("zhangli","女",1)
player3.join_room(player2)
###验证用户创建异常机制
player4=Player("zhangli","女","gsr")


################################################
#作业五：师生管理系统：
#教师和学生的共同属性：姓名，年龄和地址。私有属性：教师（薪水，课程[列表形式]和假期）、学生（成绩[字典形式]和学费）
#创建父类SchoolMember，学生和教师继承该类。
class SchoolMember():
    def __init__(self,name,age,add):
        self.name=name
        self.age=age
        self.add=add

    def _show(self):
        print(f"姓名为{self.name},年龄是{self.age}，地址是{self.add}，",end="")

class Teacher(SchoolMember):
    def __init__(self,name,age,add,salary,cla,hol):
        super().__init__( name, age, add)
        self.salary=salary
        self.cla=cla
        self.hol=hol

    def _show(self):
        print("教师的信息如下：")
        super()._show()
        print(f"薪水为{self.salary}，课程为{self.cla},假期为{self.hol}")


class Student(SchoolMember):
    def __init__(self,name,age,add,grade,tuition):
        super().__init__(name, age, add)
        self.grade = grade
        self.tuition = tuition

    def _show(self):
        print("学生的信息如下：")
        super()._show()
        print(f"成绩为{self.grade}，学费为{self.tuition}")
wen=Teacher("文瑶",18,"湖南长沙","10000",["python","java","linux"],"two days")
wen._show()
student=Student("黄欣怡",18,"湖南农业大学",{"语文":91,"数学":110,"英语":116},4500)
student._show()
